<?php
namespace App\Admin\Controllers;
use App\Admin\Models\AdPositions;
use App\Admin\Models\HomeAd;
use App\Admin\Models\HomeAdCofig;
use App\Admin\Models\HomeCarousel;
use App\Admin\Models\HomeJdPdd;
use App\Admin\Models\HomeMenu;
use App\Admin\Models\InvitecodeShares;
use App\Admin\Models\SourceMaterial;
use Common\ControllerAdBase;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
class AdController extends ControllerAdBase
{
    public function initialize()
    {
        parent::initialize();
    }


    /**
     * 公告列表
     */
    public function hotAction()
    {
        $view_home_hot = new ViewHomeHot();
        $data          = $view_home_hot->find()->toarray();
        $dataDb        = new SourceMaterial();
        foreach ($data as &$val) {
            $temp['value']      = $val['name'] . '.' . $val['id'];
            $temp['label']      = $val['name'] . '.' . $val['id'];
            $valArr[]           = $temp;
            $val['source_name'] = '';
            if (!empty($val['ids'])) {
                $name = $dataDb->find([
                    "id in ({$val['ids']})",
                    'columns' => 'id,name',
                ])->toarray();
                if (!empty($name)) {
                    $source_name = array();
                    foreach ($name as $nval) {
                        $source_name[] = $nval['name'] . '.' . $nval['id'];
                    }
                    $val['source_name']     = implode(' , ', $source_name);
                    $val['source_name_arr'] = $source_name;
                } else {
                    $val['source_name']     = '';
                    $val['source_name_arr'] = [];
                }
            }
        }
        $this->ok($data);
    }

    /**
     * 编辑公告
     */
    public function editHotAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少id');
        }
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, '缺少ids');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '缺少name');
        }

        $tmp['name'] = $this->params['name'];
        $ids         = [];
        if (isset($this->params['ids']) && !empty($this->params['ids'])) {
            foreach ($this->params['ids'] as $val) {
                $temp = explode('.', $val);
                if (isset($temp[1])) {
                    $ids[] = $temp[1];
                }
            }
        }
        if (!empty($ids)) {
            $tmp['ids'] = implode(',', $ids);
        } else {
            $tmp['ids'] = '';
        }
        $db   = new ViewHomeHot();
        $info = $db->findFirstById($this->params['id']);
        if (!$info) {
            $this->fail(-1, '不存在');
        }
        $info->name = $tmp['name'];
        $info->ids  = $tmp['ids'];
        if (!$info->save()) {
            $this->fail(-1, '保存失败!');
        }
        $this->ok();
    }

    /**
     * 弹窗列表
     */
    public function popupAction()
    {
        if (!isset($this->params['key']) || empty($this->params['key'])) {
            $this->params['key']='home';
        }
        $f_ad_positions = new AdPositions();
        $conditions = "key='{$this->params['key']}'";
        $data           = $f_ad_positions->find(array($conditions))->toarray();
        $dataDb         = new SourceMaterial();
        foreach ($data as &$val) {
            $temp['value']      = $val['name'] . '.' . $val['id'];
            $temp['label']      = $val['name'] . '.' . $val['id'];
            $valArr[]           = $temp;
            $val['source_name'] = '';
            if (!empty($val['ids'])) {
                $name = $dataDb->findFirstById($val['ids']);
                if (!empty($name)) {
                    $val['source_name'] = $name->name;
                } else {
                    $val['source_name_arr'] = [];
                }
            } else {
                $val['source_name'] = '';
            }
        }
        $this->ok($data);
    }

    /**
     * 编辑弹窗
     */
    public function editPopupAction()
    {
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, '缺少ids');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '缺少name');
        }
        if (!isset($this->params['num'])) {
            $this->fail(-1, '缺少num');
        }
        if (!isset($this->params['expire_at'])) {
            $this->fail(-1, '缺少expire_at');
        }
        $info = new AdPositions();
        if (isset($this->params['id']) && !empty($this->params['id'])) {
            $info = $info->findFirstById($this->params['id']);
            if (!$info) {
                $this->fail(-1, '不存在');
            }
        }else{
            $info->updated_at = date('Y-m-d H:i:s');
        }
        $info->name       = $this->params['name'];
        $info->ids        = $this->params['ids'];
        $info->num        = $this->params['num'];
        $info->style       = 'modal';
        $info->key = isset($this->params['key']) ? $this->params['key'] : 'home';
        $info->expire_at  = $this->params['expire_at'];
        $info->start_at  = $this->params['start_at'];
        $info->created_at  = date('Y-m-d H:i:s');
        $info->describe = $this->params['describe'];
        if (!$info->save()) {
            $this->fail(-1, '保存失败!');
        }
        $this->ok();
    }


    //删除广告
    public function ad_deleteAction(){
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '广告id丢失');
        }
        $info = new AdPositions();
        $find_one = $info->findFirstById($this->params['id']);
        if(!$find_one){
            $this->fail(-1,'没有找到广告记录');
        }
        if($find_one->delete()){
            $this->ok([]);
        }
        $this->fail(-1,'删除失败');
    }


    public function carouselAction()
    {
        $view_home_carousel = new ViewHomeCarousel();
        $list               = $view_home_carousel->find()->toarray();
        $this->ok($list);
    }

//    public function edit

    public function templatesConfigAction()
    {
        $view_home_carousel = new ViewHomeTemplatesConfig();
        $list               = $view_home_carousel->find()->toarray();
        $this->ok($list);
    }

    public function floatadAction()
    {
        $params = $this->params;
        $page   = isset($params['page']) ? intval($params['page']) : 1;
        $size   = isset($params['limit']) ? intval($params['limit']) : 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $view_home_templates_config = new ViewHomeTemplatesConfig();
        $cate_list                  = $view_home_templates_config::find(
            array(
                "order"   => 'id desc',
                'columns' => 'id,name,float_id,version,platform',
                'limit'   => $size,
                'offset'  => $page
            ))->toarray();
        if (!empty($cate_list)) {
            $sourceDb = new SourceMaterial();
            foreach ($cate_list as $key => $val) {
                if (!empty($val['float_id'])) {
                    $info = $sourceDb->find_by_id($val['float_id']);
                    if ($info) {
                        $cate_list[$key]['source_name'] = $info['name'];
                    } else {
                        $cate_list[$key]['source_name'] = '';
                    }
                } else {
                    $cate_list[$key]['source_name'] = '';
                }
            }
        }
        $result['total'] = $view_home_templates_config->count();
        $result['list']  = $cate_list;
        $this->ok($result);
    }

    public function updateFloatadAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少ID');
        }
        if (!isset($this->params['float_id']) || empty($this->params['float_id'])) {
            $this->fail(-1, '缺少float_id');
        }
        $view_home_templates_config = new ViewHomeTemplatesConfig();
        $info                       = $view_home_templates_config->findFirstById($this->params['id']);
        if (!$info) {
            $this->fail(-1, '不存在');
        }
        $info->float_id = $this->params['float_id'];
        if ($info->save()) {
            $this->ok();
        }
        $this->fail(-1, '修改失败');
    }

    public function clearrouterAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少ID');
        }
        $view_home_templates_config = new ViewHomeTemplatesConfig();
        $info                       = $view_home_templates_config->findFirstById($this->params['id']);
        if (!$info) {
            $this->fail(-1, '不存在');
        }
        $info->float_id = '';
        if ($info->save()) {
            $this->ok();
        }
        $this->fail(-1, '修改失败');
    }

    public function goods_list($params = [])
    {


    }

    /**
     * 广告主表
     */
    public function list_home_jd_pddAction()
    {
        $view_home_jd_pdd = new HomeJdPdd();
        $list             = $view_home_jd_pdd->find()->toarray();
        $adConfigDb = new HomeAdCofig();
        $carouselDb = new HomeCarousel();
        $menuDb = new HomeMenu();
        foreach($list as $key=>$value){
            $ad_config_find_one = $adConfigDb->find_by_id_obj($value['ad_id']);
            $list[$key]['ad_name'] = $ad_config_find_one ? $ad_config_find_one->name : '';

            $carousel_find_one = $carouselDb->find_by_id_obj($value['carousel_id']);
            $list[$key]['carousel_name'] = $carousel_find_one ? $carousel_find_one->name : '';

            $menu_find_one = $menuDb->findFirstById($value['menu_id']);
            $list[$key]['menu_name'] = $menu_find_one ? $menu_find_one->name : '';
        }
        $this->ok($list);
    }

    public function update_home_jd_pddAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '标签id不能为空');
        }
        $view_home_jd_pdd = new HomeJdPdd();
        $info             = $view_home_jd_pdd->find_by_id($this->params['id']);
        if (!$info) {
            $this->fail(-1, '不存在!');
        }
        if (isset($this->params['carousel_id'])) {
            $info->carousel_id = $this->params['carousel_id'];
        }
        if (isset($this->params['ad_id'])) {
            $info->ad_id = $this->params['ad_id'];
        }
        if ($info->save()) {
            $this->ok();
        }
        $this->fail(-1, '失败!');
    }

    public function list_home_carouselAction()
    {
        $view_home_carousel = new HomeCarousel();
        $list               = $view_home_carousel->find()->toarray();
        $SourceMaterial = new SourceMaterial();
        foreach ($list as $val){
            if($val['ids']){
                $sourname = $SourceMaterial->get_route_list();
            }
        }
        $this->ok($list);
    }

    public function update_home_carouselAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '标签id不能为空');
        }

        $this->db->begin();
        switch ($this->params['type']) {
            case -1:
                $this->params['type'] = 2;
                break;
            case 2:
                $this->params['type']     = 2;
                $this->params['app_path'] = 'H5UserId';
                break;
            case 3:
                $this->params['type']     = 2;
                $this->params['app_path'] = "H5";
                break;
        }


        $source_materialDb = new SourceMaterial();
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $source_materialDb->name          = $this->params['name'];
            $source_materialDb->path          = $this->params['path'];
            $source_materialDb->type          = $this->params['type'];
            $source_materialDb->app_path      = $this->params['app_path'];
            $source_materialDb->url           = $this->params['url'];
            $source_materialDb->app_data      = $this->params['app_data'];
            $source_materialDb->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_materialDb->is_need_login = $this->params['is_need_login'];
            $source_materialDb->title         = $this->params['title'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->end_color     = $this->params['end_color'];
            $source_materialDb->relationId    = $this->params['relationId'];
            if (!$source_materialDb->save()) {
                $this->db->rollback();
                $this->fail(-1, '路由创建失败');
            }
        } else {
            $source_material_find_one = $source_materialDb->find_by_id_obj($this->params['id']);
            if (!$source_material_find_one) {
                $this->db->rollback();
                $this->fail(-1, '没有找到该路由记录，该路由已经被管理员删除');
            }
            $source_material_find_one->name          = $this->params['name'];
            $source_material_find_one->path          = $this->params['path'];
            $source_material_find_one->type          = $this->params['type'];
            $source_material_find_one->app_path      = $this->params['app_path'];
            $source_material_find_one->url           = $this->params['url'];
            $source_material_find_one->app_data      = $this->params['app_data'];
            $source_material_find_one->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_material_find_one->is_need_login = $this->params['is_need_login'];
            $source_material_find_one->title         = $this->params['title'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->end_color     = $this->params['end_color'];
            $source_material_find_one->relationId    = $this->params['relationId'];
            if (!$source_material_find_one->save()) {
                $this->db->rollback();
                $this->fail(-1, '修改路由失败');
            }
        }

        $f_ad_positions = new AdPositions();
        $info           = $f_ad_positions->findFirstById($this->params['id']);
        if (!$info) {
            $this->fail(-1, '不存在');
        }

        $info->ids = $info->ids ? $info->ids . ',' . $source_material_find_one->id : $source_material_find_one->id;
        if (!$info->save()) {
            $this->rollback();
            $this->fail(-1, '操作失败!');
        }
        $this->db->commit();
        $this->ok([]);
    }


    public function list_home_ad_cofigAction()
    {
        $view_home_carousel = new HomeAdCofig();
        $list               = $view_home_carousel->find()->toarray();
        $this->ok($list);
    }

    public function list_home_adAction()
    {
        $view_home_carousel = new HomeAd();
        $list               = $view_home_carousel->find()->toarray();
        $this->ok($list);
    }


    public function posterAction(){
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $invitecode_share = new InvitecodeShares();
        $getlist = $invitecode_share->getlist();
        $paginator  = new PaginatorModel(
            array(
                "data"  => $getlist,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $this->ok($list_items);
    }


    /**
     * 添加海报
     */
    public function addupdateposterAction(){
        if (!isset($this->params['fileurl']) || empty($this->params['fileurl'])) {
            $this->fail(-1,'图片地址不能为空');
        }
        if (!isset($this->params['url']) || empty($this->params['url'])) {
            $this->fail(-1,'跳转地址不能为空');
        }
        $invitecode_share = new InvitecodeShares();
        if (isset($this->params['id']) && !empty($this->params['id'])) {
            $find_one = $invitecode_share->find_by_id($this->params['id']);
            $find_one->title = $this->params['title'];
            $find_one->url = $this->params['url'];
            $find_one->updated_at = date('Y-m-d H:i:s');
            $find_one->published = $this->params['published'];
            $find_one->fileurl = $this->params['fileurl'];
            $find_one->word = $this->params['word'];
            $find_one->classify = $this->params['classify'];
            $find_one->describe = $this->params['describe'];
            if($find_one->save()){
                $this->ok([]);
            }
        }else{
            $invitecode_share->title = $this->params['title'];
            $invitecode_share->url = $this->params['url'];
            $invitecode_share->created_at = date('Y-m-d H:i:s');
            $invitecode_share->published = $this->params['published'];
            $invitecode_share->fileurl = $this->params['fileurl'];
            $invitecode_share->classify = $this->params['classify'];
            $invitecode_share->describe = $this->params['describe'];
            $invitecode_share->word = $this->params['word'];
            if($invitecode_share->save()){
                $this->ok([]);
            }
        }
        $this->fail(-1,'失败');
    }

    /**
     * 删除海报
     */
    public function deletepostAction(){
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1,'删除id不能为空');
        }
        $invitecode_share = new InvitecodeShares();
        $find_one = $invitecode_share->find_by_id($this->params['id']);
        if(!$find_one){
            $this->fail(-1,'海报不存在');
        }
        if($find_one->delete()){
            $this->ok([]);
        }
        $this->fail(-1,'删除失败');

    }


    /**
     * 海报分类
     */
    public function posterCateAction(){
        $invitecode_share = new InvitecodeShares();
        $list = $invitecode_share->classify();
        $this->ok($list);
    }


    /**
     * 轮播列表
     */
    public function carouselListAction(){
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $homejdpdd = new HomeJdPdd();
        $getlist = $homejdpdd->findlist();
        $paginator  = new PaginatorModel(
            array(
                "data"  => $getlist,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $list_items = CommonController::transition_array($list_items);
        $carousel = new HomeCarousel();
        foreach ($list_items['items'] as $key=>$value){
            $list_items['items'][$key]['carousel_name'] =$carousel->find_by_id($value['carousel_id']) ? $carousel->find_by_id($value['carousel_id'])['name'] : '';
        }
        $this->ok($list_items);
    }


    /**
     * 修改轮播
     */
    public function updatecarouselAction(){
        //当前页数
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1,'id不能为空');
        }
        if (!isset($this->params['ca_id']) || empty($this->params['ca_id'])) {
            $this->fail(-1,'轮播id不能为空');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1,'轮播名称不能为空');
        }
        $home_carousel = new HomeCarousel();
        $find_one = $home_carousel->find_by_id($this->params['ca_id']);
        if(empty($find_one)){
            $this->fail(-1,'没有找到轮播');
        }

        $jd_pdd = new HomeJdPdd();
        $jd_pdd_find_one = $jd_pdd->find_by_id($this->params['id']);
        $jd_pdd_find_one->name = $this->params['name'];
        $jd_pdd_find_one->carousel_id = $this->params['ca_id'];
        if($jd_pdd_find_one->save()){
            $this->ok([]);
        }
        $this->fail(-1,'操作失败');
    }

    /**
     * 小程序海报
     */
    public function xcxposterAction(){
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }

        $result['items']=[];
        $this->ok($result);
    }

    /**
     * 小程序修改
     */
    public function xcxupdatePosterAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'海报id丢失');
        }
        if(!isset($this->params['fileurl']) || empty($this->params['fileurl'])){
            $this->fail(-1,'请上传海报图片');
        }
        $xcxposterDb = new XcxShares();
        $find_one = $xcxposterDb->findFirstById($this->params['id']);
        if(!$find_one){
            $this->fail(-1,'没有找到记录');
        }
        $find_one->fileurl = $this->params['fileurl'];
        $find_one->created_at = date('Y-m-d H;i:s');
        $find_one->published=1;
        if($find_one->save()){
            $this->ok([]);
        }
        $this->fail(-1,'保存失败');
    }



    public function jd_pdd_addcarouselAction(){
        if(!isset($this->params['templates_config_id']) && !empty($this->params['templates_config_id'])){
            $this->fail(-1,'没有找到记录');
        }
        $this->dbMaster->begin();
        $jd_pddDb = new HomeJdPdd();
        $jd_pdd_find_one = $jd_pddDb->find_by_id_obj($this->params['templates_config_id']);
        if(!$jd_pdd_find_one){
            $this->fail(-1,'没有找到记录');
        }
        switch ($this->params['cloumn_name']){
            case 'carousel':
                if(!isset($this->params['ad_name']) || empty($this->params['ad_name'])){
                    $this->fail(-1,'名称不能为空');
                }
                $carouselDb = new HomeCarousel();
                $carouselDb->name = $this->params['ad_name'];
                if(!$carouselDb->save()){
                    $this->dbMaster->rollback();
                    $this->fail(-1,'保存失败');
                }
                $jd_pdd_find_one->carousel_id = $carouselDb->id;
                if(!$jd_pdd_find_one->save()){
                    $this->dbMaster->rollback();
                    $this->fail(-1,'保存失败');
                }
                break;

            case 'ad1':
                if($jd_pdd_find_one->ad_id){
                    $adConfigDb = new HomeAdCofig();
                    $ad_config_find_one = $adConfigDb->find_by_id_obj($jd_pdd_find_one->ad_id);
                    if($ad_config_find_one){
                        if(!isset($this->params['ad_name']) || empty($this->params['ad_name'])){
                            $this->fail('名称不能为空');
                        }
                        $adDb = new HomeAd();
                        $adDb->name = $this->params['ad_name'];
                        $adDb->is_carousel = 0;
                        $adDb->height = $this->params['height'] ? $this->params['height'] : 80;
                        if(!$adDb->save()){
                            $this->db->rollback();
                            $this->fail(-1,'保存失败');
                        }
                        $ad_config_find_one->ids = $ad_config_find_one->ids ? $ad_config_find_one->ids.','.$adDb->id : $adDb->id;
                        if(!$ad_config_find_one->save()){
                            $this->fail(-1,'保存失败');
                        }
                    }else{
                        if(!isset($this->params['ad_name']) || empty($this->params['ad_name'])){
                            $this->fail('名称不能为空');
                        }
                        $adDb = new HomeAd();
                        $adDb->name = $this->params['ad_name'];
                        $adDb->is_carousel = 0;
                        $adDb->height = $this->params['height'] ? $this->params['height'] : 80;
                        if(!$adDb->save()){
                            $this->db->rollback();
                            $this->fail(-1,'保存失败');
                        }
                        if(!isset($this->params['ad_config_name']) || empty($this->params['ad_config_name'])){
                            $this->fail('主名称不能为空');
                        }
                        $adConfigDb = new HomeAdCofig();
                        $adConfigDb->name = $this->params['ad_config_name'];
                        $adConfigDb->ids = $adDb->id;
                        if(!$adConfigDb->save()){
                            $this->dbMaster->rollback();
                            $this->fail(-1,'保存失败');
                        }
                    }
                }else{
                    if(!isset($this->params['ad_name']) || empty($this->params['ad_name'])){
                        $this->fail('名称不能为空');
                    }
                    $adDb = new HomeAd();
                    $adDb->name = $this->params['ad_name'];
                    $adDb->is_carousel = 0;
                    $adDb->height = $this->params['height'] ? $this->params['height'] : 80;
                    if(!$adDb->save()){
                        $this->dbMaster->rollback();
                        $this->fail(-1,'保存失败');
                    }
                    if(!isset($this->params['ad_config_name']) || empty($this->params['ad_config_name'])){
                        $this->fail('主名称不能为空');
                    }
                    $adConfigDb = new HomeAdCofig();
                    $adConfigDb->name = $this->params['ad_config_name'];
                    $adConfigDb->ids = $adDb->id;
                    if(!$adConfigDb->save()){
                        $this->dbMaster->rollback();
                        $this->fail(-1,'保存失败');
                    }
                    $jd_pdd_find_one->ad_id = $adConfigDb->id;
                    if(!$jd_pdd_find_one->save()){
                        $this->dbMaster->rollback();
                        $this->fail(-1,'保存失败');
                    }
                }
                break;
            default:
                $this->fail(-1,'模块错误');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }




    /**
     * 个人中心选择路由
     */
    public function self_center_confirm_routerAction()
    {
        if (!isset($this->params['columns_name']) || empty($this->params['columns_name'])) {
            $this->fail(-1, '模块没有找到');
        }
        if (!isset($this->params['templateconfig_id']) || empty($this->params['templateconfig_id'])) {
            $this->fail(-1, '模块没有找到');
        }
        if (!isset($this->params['router_columns_id']) || empty($this->params['router_columns_id'])) {
            $this->fail(-1, '没有找到当前路由');
        }
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, '您没有选择路由');
        }
        $selfCenterDb      = new ViewHomeSelfCenter();
        $template_find_one = $selfCenterDb->find_by_id_obj($this->params['templateconfig_id']);
        if (!$template_find_one) {
            $this->fail(-1, '没有找到当前版本');
        }
        $ids = implode(',', $this->params['ids']);
        switch ($this->params['columns_name']) {
            case 'center':

                break;

            case 'ad1':
                $adDb        = new HomeAd();
                $ad_find_one = $adDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$ad_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if($ad_find_one->ids){
                    $ids_new = explode(',',$ids);
                    $ids_old = explode(',',$ad_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new,$ids_old));
                    $ids = implode(',',$ids_arr);
                }
                $ad_find_one->ids = $ids;
                if (!$ad_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;

            case 'ad2':
                $adDb        = new HomeAd();
                $ad_find_one = $adDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$ad_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if($ad_find_one->ids){
                    $ids_new = explode(',',$ids);
                    $ids_old = explode(',',$ad_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new,$ids_old));
                    $ids = implode(',',$ids_arr);
                }
                $ad_find_one->ids = $ids;
                if (!$ad_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;

            case 'menu':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$menu_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if($menu_find_one->ids){
                    $ids_new = explode(',',$ids);
                    $ids_old = explode(',',$menu_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new,$ids_old));
                    $ids = implode(',',$ids_arr);
                }
                $menu_find_one->ids = $ids;
                if (!$menu_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;

            case 'menu1':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$menu_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if($menu_find_one->ids){
                    $ids_new = explode(',',$ids);
                    $ids_old = explode(',',$menu_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new,$ids_old));
                    $ids = implode(',',$ids_arr);
                }
                $menu_find_one->ids = $ids;
                if (!$menu_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;
            default:
                $this->fail(-1, '模块错误');
        }
        $this->ok([]);
    }




    /**
     * 选择添加的路由
     */
    public function confirm_routerAction()
    {
        if (!isset($this->params['templates_config_id']) || empty($this->params['templates_config_id'])) {
            $this->fail(-1, '对不起，没有找到版本号');
        }
        if (!isset($this->params['router_cloumn_id']) || empty($this->params['router_cloumn_id'])) {
            $this->fail(-1, '对不起，没有找到模板');
        }
        if (!isset($this->params['cloumn_name']) || empty($this->params['cloumn_name'])) {
            $this->fail(-1, '对不起,没有找到模板名称');
        }
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, '请选择路由');
        }
        $ids             = implode(',', $this->params['ids']);
        $templatesconfig = new HomeJdPdd();
        $templates       = $templatesconfig->find_by_id_obj($this->params['templates_config_id']);
        if (empty($templates)) {
            $this->fail(-1, '对不起，当前版本号不存在，或者已经被删除');
        }
        switch ($this->params['cloumn_name']) {
            case 'carousel':
                $carouselDb        = new HomeCarousel();
                $carousel_find_one = $carouselDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$carousel_find_one) {
                    $this->fail(-1, '没有找到轮播记录');
                }
                if ($carousel_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $carousel_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $carousel_find_one->ids = $ids;
                if (!$carousel_find_one->save()) {
                    $this->fail(-1, '修改失败');
                }
                break;
            case 'search_bar':
                if (count($this->params['ids']) >= 3) {
                    $this->fail(-1, '搜索菜单只能设置两个');
                }
                $searchBarDb         = new HomeSearchBar();
                $search_bar_find_one = $searchBarDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$search_bar_find_one) {
                    $this->fail(-1, '没有找到搜索记录');
                }
                if ($search_bar_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $search_bar_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $search_bar_find_one->ids = $ids;
                if (!$search_bar_find_one->save()) {
                    $this->fail(-1, '修改失败');
                }
                break;
            case 'menu':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->fail(-1, '没有找到菜单记录');
                }
                if ($menu_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $menu_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $menu_find_one->ids = $ids;
                if (!$menu_find_one->save()) {
                    $this->fail(-1, '修改失败');
                }
                break;
            case 'ad1':
                $ad1Db        = new HomeAd();
                $ad1_find_one = $ad1Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad1_find_one) {
                    $this->fail(-1, '没有找到广告记录');
                }
                if ($ad1_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $ad1_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $ad1_find_one->ids = $ids;
                if (!$ad1_find_one->save()) {
                    $this->fail(-1, '修改失败');
                }
                break;
            case 'ad2':
                $ad2Db        = new HomeAd();
                $ad2_find_one = $ad2Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad2_find_one) {
                    $this->fail(-1, '没有找到广告2记录');
                }
                if ($ad2_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $ad2_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $ad2_find_one->ids = $ids;
                if (!$ad2_find_one->save()) {
                    $this->fail(-1, '修改失败');
                }
                break;
            default:
                $this->fail(-1, '模块错误');
                break;
        }
        $this->ok([]);
    }





    /**
     * 添加路由
     */
    public function routeraddAction()
    {
        if (!isset($this->params['templates_config_id']) || empty($this->params['templates_config_id'])) {
            $this->fail(-1, '对不起，没有找到版本号');
        }
        if (!isset($this->params['router_cloumn_id']) || empty($this->params['router_cloumn_id'])) {
            $this->fail(-1, '对不起，没有找到模板');
        }
        if (!isset($this->params['cloumn_name']) || empty($this->params['cloumn_name'])) {
            $this->fail(-1, '对不起,没有找到模板名称');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '路由名称不能为空');
        }
        $this->db->begin();


        $templatesconfig = new HomeJdPdd();
        $templates       = $templatesconfig->find_by_id($this->params['templates_config_id']);
        if (empty($templates)) {
            $this->fail(-1, '对不起，当前版本号不存在，或者已经被删除');
        }

        switch ($this->params['type']) {
            case -1:
                $this->params['type'] = 2;
                break;
            case 2:
                $this->params['type']     = 2;
                $this->params['app_path'] = 'H5UserId';
                break;
            case -2:
                $this->params['type']     = 2;
                $this->params['app_path'] = "H5";
                break;
        }

        $source_materialDb = new SourceMaterial();
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $source_materialDb->name          = $this->params['name'];
            $source_materialDb->path          = $this->params['path'];
            $source_materialDb->type          = $this->params['type'];
            $source_materialDb->app_path      = $this->params['app_path'];
            $source_materialDb->url           = $this->params['url'];
            $source_materialDb->app_data      = $this->params['app_data'];
            $source_materialDb->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_materialDb->is_need_login = $this->params['is_need_login'];
            $source_materialDb->title         = $this->params['title'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->end_color     = $this->params['end_color'];
            $source_materialDb->relationId    = $this->params['relationId'];
            if (!$source_materialDb->save()) {
                $this->db->rollback();
                $this->fail(-1, '路由创建失败');
            }
        } else {
            $source_material_find_one = $source_materialDb->find_by_id_obj($this->params['id']);
            if (!$source_material_find_one) {
                $this->db->rollback();
                $this->fail(-1, '没有找到该路由记录，该路由已经被管理员删除');
            }
            $source_material_find_one->name          = $this->params['name'];
            $source_material_find_one->path          = $this->params['path'];
            $source_material_find_one->type          = $this->params['type'];
            $source_material_find_one->app_path      = $this->params['app_path'];
            $source_material_find_one->url           = $this->params['url'];
            $source_material_find_one->app_data      = $this->params['app_data'];
            $source_material_find_one->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_material_find_one->is_need_login = $this->params['is_need_login'];
            $source_material_find_one->title         = $this->params['title'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->end_color     = $this->params['end_color'];
            $source_material_find_one->relationId    = $this->params['relationId'];
            if (!$source_material_find_one->save()) {
                $this->db->rollback();
                $this->fail(-1, '修改路由失败');
            }
        }
        switch ($this->params['cloumn_name']) {
            case 'carousel':
                $carouselDb        = new HomeCarousel();
                $carousel_find_one = $carouselDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$carousel_find_one) {
                    $this->db->rollback();
                    $this->fail(-1, '没有找到轮播记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($carousel_find_one->ids)) {
                        $carousel_find_one->ids = $source_materialDb->id;
                    } else {
                        $carousel_find_one->ids = $carousel_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$carousel_find_one->save()) {
                        $this->db->rollback();
                        $this->fail(-1, '修改轮播记录失败');
                    }
                }
                break;
            case 'search_bar':
                $searchBarDb         = new HomeSearchBar();
                $search_bar_find_one = $searchBarDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$search_bar_find_one) {
                    $this->db->rollback();
                    $this->fail(-1, '没有找到搜索记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($search_bar_find_one->ids)) {
                        $search_bar_find_one->ids = $source_materialDb->id;
                    } else {
                        $search_bar_find_one->ids = $search_bar_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$search_bar_find_one->save()) {
                        $this->db->rollback();
                        $this->fail(-1, '修改搜索记录失败');
                    }
                }
                break;
            case 'menu':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->db->rollback();
                    $this->fail(-1, '没有找到菜单记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($menu_find_one->ids)) {
                        $menu_find_one->ids = $source_materialDb->id;
                    } else {
                        $menu_find_one->ids = $menu_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$menu_find_one->save()) {
                        $this->db->rollback();
                        $this->fail(-1, '修改菜单记录失败');
                    }
                }
                break;
            case 'menu1':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->db->rollback();
                    $this->fail(-1, '没有找到菜单记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($menu_find_one->ids)) {
                        $menu_find_one->ids = $source_materialDb->id;
                    } else {
                        $menu_find_one->ids = $menu_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$menu_find_one->save()) {
                        $this->db->rollback();
                        $this->fail(-1, '修改菜单记录失败');
                    }
                }
                break;
            case 'ad1':
                $ad1Db        = new HomeAd();
                $ad1_find_one = $ad1Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad1_find_one) {
                    $this->db->rollback();
                    $this->fail(-1, '没有找到广告记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($ad1_find_one->ids)) {
                        $ad1_find_one->ids = $source_materialDb->id;
                    } else {
                        $ad1_find_one->ids = $ad1_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$ad1_find_one->save()) {
                        $this->db->rollback();
                        $this->fail(-1, '修改广告记录失败');
                    }
                }
                break;
            case 'ad2':
                $ad2Db        = new HomeAd();
                $ad2_find_one = $ad2Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad2_find_one) {
                    $this->db->rollback();
                    $this->fail(-1, '没有找到广告2记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($ad2_find_one->ids)) {
                        $ad2_find_one->ids = $source_materialDb->id;
                    } else {
                        $ad2_find_one->ids = $ad2_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$ad2_find_one->save()) {
                        $this->db->rollback();
                        $this->fail(-1, '修改广告2记录失败');
                    }
                }
                break;
            default:
                $this->fail(-1, '模块错误');
                break;
        }
        $this->db->commit();
        $this->ok([]);
    }





}
